package cn.alise.controller.common;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;

import cn.alise.consts.BaseConsts;
import cn.alise.qdp.model.common.Progress;
import cn.alise.util.JSONResult;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;


/**
 * 通用管理控制器
 */
@Controller
@RequestMapping(value = "/global")
public class GlobalController extends BaseController {
	
	private static final long serialVersionUID = 8067293756668444590L;

	/**
	 * 国际化切换
	 */
	@RequestMapping(value = "/change", method = {RequestMethod.GET})
	public String change(HttpServletRequest request, HttpServletResponse response, Model model, @RequestParam(value="locale", defaultValue="en_US") String locale) {
		
		if (locale.equals("zh_CN")) {
            Locale local = new Locale("zh", "CN"); 
            (new CookieLocaleResolver()).setLocale (request, response, local);
        } else if(locale.equals("en_US")) {
            Locale local = new Locale("en", "US"); 
            (new CookieLocaleResolver()).setLocale (request, response, local);
        } else {
        	(new CookieLocaleResolver()).setLocale (request, response, LocaleContextHolder.getLocale());
        }
            
        return "pages/common/home";
	}
	
	/**
	 * 导出进度获取
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/getExportProgress")  
	public void getExportProgress(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		JSONResult result = new JSONResult();
		
		//session 获取当前用户
		String userSession = this.getAccountFromSession().getUserId().toString();
		
		Map<String, Long> mapTotal = new HashMap<String, Long>();
		Map<String, Long> mapValue = new HashMap<String, Long>();
		
		// 从session中获取状态
		long total = 0;
		long value = 0;
		if (this.getSesionValue(BaseConsts.IUser.EXPORT_PROGRESS_TOTAL) != null) {
			mapTotal = (HashMap<String, Long>)this.getSesionValue(BaseConsts.IUser.EXPORT_PROGRESS_TOTAL);
			total = mapTotal.get(userSession + "total");
		}
		if (this.getSesionValue(BaseConsts.IUser.EXPORT_PROGRESS_VALUE) != null) {
			mapValue = (HashMap<String, Long>)this.getSesionValue(BaseConsts.IUser.EXPORT_PROGRESS_VALUE);
			value = mapValue.get(userSession + "value");
		}
		Progress data = new Progress();
		data.setpContentLength(total);
		data.setpBytesRead(value);
		
		result.setData(JSON.toJSON(data));
		result.setStatus(0);
		result.setMessage("返回进度！");
		getPrintWriter(response,JSONArray.toJSONString(result));
		
	}
	
	/**
	 * 导入进度获取
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/getImportProgress")  
	public void getImportProgress(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		JSONResult result = new JSONResult();
		
		//session 获取当前用户
		String userSession = this.getAccountFromSession().getUserId().toString();
		
		Map<String, Long> mapTotal = new HashMap<String, Long>();
		Map<String, Long> mapValue = new HashMap<String, Long>();
		Map<String, Long> mapSuccessNum = new HashMap<String, Long>();
		Map<String, Long> mapFailNum = new HashMap<String, Long>();
		
		// 从session中获取状态
		long total = 0;
		long value = 0;
		long successNum = 0;
		long failNum = 0;
		
		if (this.getSesionValue(BaseConsts.IUser.IMPORT_PROGRESS_TOTAL) != null) {
			mapTotal = (HashMap<String, Long>)this.getSesionValue(BaseConsts.IUser.IMPORT_PROGRESS_TOTAL);
			total = mapTotal.get(userSession + "total");
		}
		if (this.getSesionValue(BaseConsts.IUser.IMPORT_PROGRESS_VALUE) != null) {
			mapValue = (HashMap<String, Long>)this.getSesionValue(BaseConsts.IUser.IMPORT_PROGRESS_VALUE);
			value = mapValue.get(userSession + "value");
		}
		if (this.getSesionValue(BaseConsts.IUser.IMPORT_SUCCESS_NUM) != null) {
			mapSuccessNum = (HashMap<String, Long>)this.getSesionValue(BaseConsts.IUser.IMPORT_SUCCESS_NUM);
			successNum = mapSuccessNum.get(userSession + "successNum");
		}
		if (this.getSesionValue(BaseConsts.IUser.IMPORT_FAIL_NUM) != null) {
			mapFailNum = (HashMap<String, Long>)this.getSesionValue(BaseConsts.IUser.IMPORT_FAIL_NUM);
			failNum = mapFailNum.get(userSession + "failNum");
		}
		
		Progress data = new Progress();
		data.setpContentLength(total);
		data.setpBytesRead(value);
		data.setSuccessNum(successNum);
		data.setFailNum(failNum);
		
		result.setData(JSON.toJSON(data));
		result.setStatus(0);
		result.setMessage("返回进度！");
		
		getPrintWriter(response,JSONArray.toJSONString(result));
		
	}
}